﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp4
{
    class DbHelperBySql
    {
        public static readonly string conString = "server=.;database=Blogs;uid=sa;pwd=123;";
        /// <summary>
        /// 获得表数据
        /// </summary>
        /// <param name="sql">查询的select语句</param>
        /// <returns>表数据</returns>
        public static DataTable GetDataTable(string sql)
        {           
            SqlConnection sqlConnection = new SqlConnection(conString);
            sqlConnection.Open();

            using (SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlConnection))
            {
                DataTable dt = new DataTable();
                dataAdapter.Fill(dt);
                return dt;
            }

        }


        /// <summary>
        /// 增加或修改的方法
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns> 
        public static int AddOrEditOrDeleteSave(string sql)
        {
            SqlConnection sqlConnection = new SqlConnection(conString);  //初始化连接对象

            //初始化命令对象
            using (SqlCommand command = new SqlCommand(sql, sqlConnection))
            {
                sqlConnection.Open();
                //返回受影响的行数
                return command.ExecuteNonQuery();
            }         
        }
    }
}
